﻿namespace Windows32
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;

    public class GDIPen : GDIObject
    {
        private System.Drawing.Color intColor;
        private PenStyle intStyle;
        private int intWidth;
        public static readonly GDIPen NullPen = new GDIPen(PenStyle.PS_NULL, 1, System.Drawing.Color.Black);

        public GDIPen(System.Drawing.Color color) : this(PenStyle.PS_SOLID, 1, color)
        {
        }

        public GDIPen(int width, System.Drawing.Color color) : this(PenStyle.PS_SOLID, width, color)
        {
        }

        public GDIPen(PenStyle style, int width, System.Drawing.Color color)
        {
            this.intStyle = PenStyle.PS_SOLID;
            this.intWidth = 1;
            this.intColor = System.Drawing.Color.Black;
            this.intStyle = style;
            this.intWidth = width;
            this.intColor = color;
            base.intHandle = CreatePen((int) this.intStyle, this.intWidth, base.ColorToInt(color));
        }

        [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
        private static extern IntPtr CreatePen(int PenStyle, int Width, int Color);
        public void DrawLine(IntPtr hdc, Point p1, Point p2)
        {
            this.DrawLine(hdc, p1.X, p1.Y, p2.X, p2.Y);
        }

        public void DrawLine(IntPtr hdc, int x1, int y1, int x2, int y2)
        {
            IntPtr h = base.SelectTo(hdc);
            MoveToEx(hdc, x1, y1, 0);
            LineTo(hdc, x2, y2);
            if (h.ToInt32() != 0)
            {
                base.UnSelect(hdc, h);
            }
        }

        public void DrawLines(IntPtr hdc, Point[] ps)
        {
            IntPtr h = base.SelectTo(hdc);
            POINT[] ps2 = new POINT[ps.Length];
            for (int iCount = 0; iCount < ps.Length; iCount++)
            {
                ps2[iCount].x = ps[iCount].X;
                ps2[iCount].y = ps[iCount].Y;
            }
            Polyline(hdc, ps2, ps2.Length);
            if (h.ToInt32() != 0)
            {
                base.UnSelect(hdc, h);
            }
        }

        [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
        private static extern bool LineTo(IntPtr hDC, int X, int Y);
        [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
        private static extern bool MoveToEx(IntPtr hDC, int X, int Y, int lpPoint);
        [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
        private static extern bool Polyline(IntPtr hDC, POINT[] ps, int len);

        public System.Drawing.Color Color
        {
            get
            {
                return this.intColor;
            }
            set
            {
                if (this.intColor != value)
                {
                    this.intColor = value;
                    this.Dispose();
                    base.intHandle = CreatePen((int) this.intStyle, this.intWidth, base.ColorToInt(this.intColor));
                }
            }
        }

        public PenStyle Style
        {
            get
            {
                return this.intStyle;
            }
            set
            {
                if (this.intStyle != value)
                {
                    this.intStyle = value;
                    this.Dispose();
                    base.intHandle = CreatePen((int) this.intStyle, this.intWidth, base.ColorToInt(this.intColor));
                }
            }
        }

        public int Width
        {
            get
            {
                return this.intWidth;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }
    }
}

